// 根据id type 查询 期刊信息
const { Movie, Sentence, Music } = require('./classic')
const { Op } = require('sequelize')
const { flatten } = require('lodash')

class Art {
    // constructor(art_id, type) {
    //     this.art_id = art_id
    //     this.type = type
    // }

    // 获取收藏
    static async getList(artInfoList) {
        const artInfoObj = {
            100: [],
            200: [],
            300: []
        }
        for (let artInfo of artInfoList) {
            artInfoObj[artInfo.type].push(artInfo.art_id)
        }

        const arts = [];
        // 循环查询
        for (let key in artInfoObj) {
            const ids = artInfoObj[key]
            if (ids.length === 0) {
                continue
            }
            // key obj的属性  所有obj 对象的属性都是字符串
            key = parseInt(key)
            arts.push(await Art._getListByType(ids, key))
        }

        return flatten(arts);
    }

    static async _getListByType(ids, type) {
        let arts = null;
        const finder = {
            where: {
                id: {
                    [Op.in]: ids
                }
            }
        }
        const scopt = 'bh';
        switch (type) {
            case 100:
                arts = await Movie.scope(scopt).findAll(finder)
                break;
            case 200:
                arts = await Music.scope(scopt).findAll(finder)
                break;
            case 300:
                arts = await Sentence.scope(scopt).findAll(finder)
                break;
            case 400:

                break;
            default:
                break;
        }
        // if (art && art.image) {
        //     let imgUrl = art.dataValues.image
        //     art.dataValues.image = global.config.host + imgUrl
        // }
        return arts;
    }


    static async getData(art_id, type, userScope = true) {
        let art = null;
        const finder = {
            where: {
                id: art_id,
            }
        }
        const scopt = userScope ? 'bh' : null;
        switch (type) {
            case 100:
                art = await Movie.scope(scopt).findOne(finder)
                break;
            case 200:
                art = await Music.scope(scopt).findOne(finder)
                break;
            case 300:
                art = await Sentence.scope(scopt).findOne(finder)
                break;
            case 400:
                // 书籍
                // 书籍
                const { Book } = require('./book')
                // 先查询 如果没有 就创建
                art = await Book.scope(scopt).findOne(finder)
                if (!art) {
                    art = await Book.create({
                        id: art_id
                    })
                }
                break;
            default:
                break;
        }

        return art;
    }

}


module.exports = {
    Art
}